*****************************************************************************
*               Cleaning and Analyzing - Switching vote US                  *
*  																            *
* Author: 			Valentina Gonzalez-Rostani		        				*
* Contact: 			mag384@pitt.edu              							*
* Date: 			March 21 2021 									*
* Version:			Stata 17						                        *
*																			*
*****************************************************************************
/*
This do-file:
	* Processing of the data
		* Call the Data
		* Define variables
		* Save the data
	* Load preapred data **line 794**
	* Analysis and Descriptives: Export Tables &  Figure

Input: GSS data
	- Data\Switching\GSS7218_R3.dta

Final output:
	Cleaned data: 
		* "Data\GSS.dta" this data contains the relevant variables for the analysis.
	Tables:
		* table A3: Switching Vote, IV - RTI, US [Table\USlong_2.tex]
		* table A5: Switching Vote (alternative definition), IV - RTI [Table\USStrict_2.tex]
		* table A6: Switching Vote, IV - Routine (dummy), US [Table\USdummy_2.tex]
		* table A1: Descriptive statistic: USA GSS 2016 vs 2012
	Figure:
	    * Figure 3: The effect of exposure to automation on vote-switching. [US Part]  [Figure\US.gph]


*/


** Set directory
cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication"

*##########################################
* Processing of the data (alternatively skip and go to line 794)
*##########################################
{
*##########################################
* Calling the data
*##########################################
{
* Clearing and allowing multiple variables
* Set the directory
* If re-running clear mata and clear matrix
clear
set maxvar 30000

* Calling the data
use "Data\Switching\GSS7218_R3.dta", clear
}
*############################################
* Creating Variables
*############################################
{
************************************************************************
********************* B. Demographic  **********************************
************************************************************************
{
* Generate a dummy variable for unemployment status
gen unemployed = (wrkstat==4)

* Generate a dummy variable for female
gen female=(sex==2)

* Generate a dummy variable for black race
gen black = (race==2)

* Dummy for born outside the US
gen foreign = .  // Initialize the variable 'foreign' with missing values
replace foreign=1 if born==2
replace foreign=0 if born==1

* Dummy for nonreligion
gen nonrelig=.
replace nonrelig=1 if relig==4
replace nonrelig=0 if relig~=4 & relig~=.

}
************************************************************************
********************* C. Standarizing occupations **********************
************************************************************************
{
*/ In this section I will standarize occupations. I follow Thewissen and Rueda (2019)  code, which was based on Harry Ganzeboom's correspondence file, who used ILO correspondence table.

* using code from Harry Ganzeboom, I convert occupations from ISCO-08 to ISCO-88. 
* I am doing this conversion because the RTI is in 08 ISCO code. 
gen iscoco = isco88

*/ Given that Goos et al (2014) code is designed for 2-digit. I create a new variable that will transform iscoco into a 2 digit variables. 

foreach var of varlist iscoco isco08 {
gen str_`var'=string(`var')
replace str_`var'="0100" if str_`var'=="100"
}
sum iscoco if str_iscoco=="0100"


foreach x in iscoco {
gen `x'2 = substr(str_`x',1,2) if `x'<9334
replace `x'2 = "." if `x'2==""
}

drop str_*

destring iscoco2, replace

label var iscoco2 "ISCO88 at the 2-digit"

* Labels Iscoco
{
gen iscoco2n="."
replace iscoco2n="Legislators and senior officials" if iscoco2==11
	replace iscoco2n="Corporate managers" if iscoco2==12
	replace iscoco2n="General managers" if iscoco2==13
	replace iscoco2n="Physical, mathematical and engineering science professionals" if iscoco2==21
	replace iscoco2n="Life science and health professionals" if iscoco2==22
	replace iscoco2n="Teaching professionals" if iscoco2==23
	replace iscoco2n="Other professionals" if iscoco2==24
	replace iscoco2n="Physical and engineering science associate professionals" if iscoco2==31
	replace iscoco2n="Life science and health associate professionals" if iscoco2==32
	replace iscoco2n="Teaching associate professionals" if iscoco2==33
	replace iscoco2n="Other associate professionals" if iscoco2==34
	replace iscoco2n="Office clerks" if iscoco2==41
	replace iscoco2n="Customer services clerks" if iscoco2==42
	replace iscoco2n="Personal and protective services workers" if iscoco2==51
	replace iscoco2n="Models, salespersons and demonstrators" if iscoco2==52
	replace iscoco2n="Market-oriented skilled agricultural and fishery workers" if iscoco2==61
	replace iscoco2n="Subsistence agricultural and fishery workers" if iscoco2==62
	replace iscoco2n="Extraction and building trades workers" if iscoco2==71
	replace iscoco2n="Metal, machinery and related trades workers" if iscoco2==72
	replace iscoco2n="Precision, handicraft, printing and related trades workers" if iscoco2==73
	replace iscoco2n="Other craft and related trades workers" if iscoco2==74
	replace iscoco2n="Stationary-plant and related operators" if iscoco2==81
	replace iscoco2n="Machine operators and assemblers" if iscoco2==82
	replace iscoco2n="Drivers and mobile-plant operators" if iscoco2==83
	replace iscoco2n="Sales and services elementary occupations" if iscoco2==91
	replace iscoco2n="Agricultural, fishery and related labourers" if iscoco2==92
	replace iscoco2n="Labourers in mining, construction, manufacturing and transport" if iscoco2==93
	replace iscoco2n="Armed forces" if iscoco2==01
label var iscoco2n "Names of iscoco2 coding"
}





************************************************************************
********************* Exposure to automation ***************************
************************************************************************
{
************************************************************************
* 1. Goos et al.(2014)
************************************************************************
{
** These lines include the routinisation and offshoring indices at the 2-digit level from Goos et al. (2014)

*/ The idea is that for each occupation (at the 2-digit level) one risk is assigned. The weak part of this index is that it treats as the same the last 2-digit of the ISCO.
* In other words, ISCO has 4 digit, but goos et al only consider the first 2 digit, therefore there is no differentiation among occupations that differ in the last 2 digit. 
gen rti=.

{
replace rti=-0.75 if iscoco2==12
replace rti=-0.82 if iscoco2==21
replace rti=-1.00 if iscoco2==22
replace rti=-0.73 if iscoco2==24
replace rti=-1.52 if iscoco2==13
replace rti=-0.40 if iscoco2==31
replace rti=-0.44 if iscoco2==34
replace rti=-0.33 if iscoco2==32

replace rti=0.32 if iscoco2==81
replace rti=0.46 if iscoco2==72
replace rti=-1.50 if iscoco2==83
replace rti=2.24 if iscoco2==41
replace rti=1.59 if iscoco2==73
replace rti=-0.19 if iscoco2==71
replace rti=1.41 if iscoco2==42
replace rti=0.49 if iscoco2==82
replace rti=1.24 if iscoco2==74

replace rti=0.45 if iscoco2==93
replace rti=-0.60 if iscoco2==51
replace rti=0.05 if iscoco2==52
replace rti=0.03 if iscoco2==91
}
label var rti "RTI index"

}
************************************************************************
***** 2. Oesch (2006) and Kitschelt and Rehm (2014) classification *****
************************************************************************
{
*Now is the same, but using Oesch (2006) and Kitschelt and Rehm (2014) classification

*/  Oesch (2006) develops the bases of a new class schema that partly shifts its focus from hierarchical divisions to horizontal cleavages. The idea is  that the middle class is not conceptualized as a unitary grouping and the manual/non-manual divide is not used as a decisive class boundary. 
*/ The emphasis is put on differences in marketable skills and the work logic. 

// What is the logic? 
*/Logic of task structures: t1 organizational (taskorg), t2 technical (tasktech), and t3 interpersonal (taskinter)
*/Logic of authority: a1 professional (authprof), a2 associate professional (authassoc), a3 skilled routine (authskil) a4 unskilled routine (authunsk)
*/Leads to 3*4 groups, in regressions of K&R combined to 4 groups (skilled+unskilled routine all tasks; prof+assoc prof for 3 tasks separately)

* Code from Thewissen and Rueda  (2019)
{
*/t1a1: Higher grade managers
gen t1a1=1 if iscoco>=1000 & iscoco<=1251 | iscoco>=2410 & iscoco<=2419 | inlist(iscoco,2441,2470)
label var t1a1 "Higher grade managers"

*/t1a2: Associate managers
gen t1a2=1 if iscoco>=1252 & iscoco<=1319 | iscoco>=3410 & iscoco<=3449 | inlist(iscoco,3452)
label var t1a2 "Associate managers"

*/t1a3: Skilled office
gen t1a3=1 if iscoco>=4000 & iscoco<=4112 | iscoco>=4114 & iscoco<=4141 | inlist(iscoco,4143) | iscoco>=4190 & iscoco<=4210 | iscoco>=4213 & iscoco<=4221
label var t1a3 "Skilled office"

*/t1a4: Routine office
gen t1a4=1 if inlist(iscoco,4113,4142,4144) | iscoco>=4211 & iscoco<=4212 | iscoco>=4222 & iscoco<=4223
label var t1a4 "Routine office"

*/t2a1: Technical experts
gen t2a1=1 if iscoco>=2100 & iscoco<=2213
label var t2a1 "Technical experts"

*/t2a2: Technicians
gen t2a2=1 if iscoco>=3100 & iscoco<=3213 | inlist(iscoco,3471)
label var t2a2 "Technicians"

*/t2a3: Skilled crafts
gen t2a3=1 if inlist(iscoco,110,8311,8324,8333) | iscoco>=7120 & iscoco<=7142 | iscoco>=7200 & iscoco<=7233 | iscoco>=7240 & iscoco<=7423 | iscoco>=7430 & iscoco<=7520
label var t2a3 "Skilled crafts"

*/t2a4: Routine operatives/agriculture
gen t2a4=1 if inlist(iscoco,7143, 7234, 7424, 8312) | iscoco>=7100 & iscoco<=7113 | iscoco>=7129 & iscoco<=7130 | iscoco>=8000 & iscoco<=8310 | iscoco>=8334 & iscoco<=8400 | iscoco>=9160 & iscoco<=9162 | iscoco>=9300 & iscoco<=9333
replace t2a4=1 if iscoco>=6010 & iscoco<=6210 | iscoco>=8330 & iscoco<=8332 | iscoco>=9200 & iscoco<=9213
label var t2a4 "Routine operatives/agriculture"

*/t3a1: Socio-cultural professionals
gen t3a1=1 if inlist(iscoco, 2359, 2445, 2451, 2460) | iscoco>=2220 & iscoco<=2323 | iscoco>=2350 & iscoco<=2351 |iscoco>=2420 & iscoco<=2440 | iscoco>=2442 & iscoco<=2443
label var t3a1 "Socio-cultural professionals"

*/t3a2: Socio-cultural semi-professionals
gen t3a2=1 if inlist(iscoco,2352, 2444, 3220, 3226) | iscoco>=2330 & iscoco<=2340 | iscoco>=2446 & iscoco<=2450 | iscoco>=2452 & iscoco<=2455 | iscoco>=3222 & iscoco<=3224 | iscoco>=3229 & iscoco<=3232 | iscoco>=3240 & iscoco<=3400 | iscoco>=3450 & iscoco<=3451 | iscoco>=3460 & iscoco<=3470 | iscoco>=3472 & iscoco<=3480
label var t3a2 "Socio-cultural semi-professionals"

*/t3a3: Skilled service
gen t3a3=1 if inlist(iscoco, 3221, 3225, 5122, 5141, 5143, 8323) | iscoco>=3227 & iscoco<=3228 | iscoco>=5110 & iscoco<=5113 | iscoco>=5150 & iscoco<=5163 | iscoco>=5200 & iscoco<=5210
label var t3a3 "Skilled service"

*/t3a4: Routine service
gen t3a4=1 if inlist(iscoco,5142, 5149, 5169) | iscoco>=5120 & iscoco<=5121 | iscoco>=5123 & iscoco<=5130 | iscoco>=5131 & iscoco<=5140 | iscoco>=5220 & iscoco<=5230 | iscoco>=8320 & iscoco<=8322 | iscoco>=9100 & iscoco<=9153
label var t3a4 "Routine service"


forvalues i=1(1)3 {
	gen t`i'=1 if t`i'a1==1 | t`i'a2==1  | t`i'a3==1 | t`i'a4==1
	}
list t1-t3 if t1==t2 & t2==t3 & t1==t3 & t1~=.
replace t1=0 if t2==1 | t3==1
replace t2=0 if t1==1 | t3==1
replace t3=0 if t1==1 | t2==1

label var t1 "Organisational task structure (t1a1, 2, 3, or 4==1)"
label var t2 "Technical task structure (t2a1, 2, 3, or 4==1)"
label var t3 "Interpersonal task structure (t3a1, 2, 3, or 4==1)"

forvalues i=1(1)4 {
	gen a`i'=1 if t1a`i'==1 | t2a`i'==1  | t3a`i'==1
	}
replace a1=0 if a2==1 | a3==1 | a4==1
replace a2=0 if a1==1 | a3==1 | a4==1
replace a3=0 if a1==1 | a2==1 | a4==1
replace a4=0 if a1==1 | a2==1 | a3==1

label var a1 "Professional authority (t1a1, t2a1, or t3a1==1)"
label var a2 "Assoc prof authority (t1a2, t2a2, or t3a2==1)"
label var a3 "Skilled routine authority (t1a3, t2a3, or t3a3==1)"
label var a4 "Unskilled routine authority (t1a4, t2a4, or t3a4==1)"

gen check = a1+a2+a3+a4
sum check
drop check

gen c1=1 if t1a1==1 | t1a2==1
gen c2=1 if t2a1==1 | t2a2==1
gen c3=1 if t3a1==1 | t3a2==1
gen c4=1 if t1a3==1 | t1a4==1 | t2a3==1 | t2a4==1 | t3a3==1 | t3a4==1

replace c1=0 if c2==1 | c3==1 | c4==1
replace c2=0 if c1==1 | c3==1 | c4==1
replace c3=0 if c1==1 | c2==1 | c4==1
replace c4=0 if c1==1 | c2==1 | c3==1

label var c1 "Skilled organisational (t1a1 or t1a2==1)"
label var c2 "Skilled technical (t2a1 or t2a2==1)"
label var c3 "Skilled interpersonal (t3a1 or t3a2==1)"
label var c4 "Unskilled routine (t1a3, t1a4, t2a3, t2a4, t3a3, or t3a4==1)"

gen check = c1+c2+c3+c4
sum check
drop check
}
}
*********************************************************************
***** 3. Offshoring index from Walter (2017), based on Blinder*******
*********************************************************************
{
*/ Stefanie Walter (2017) provides the code for offshorability based on Blinder. It uses ISCO with 4 digits. 

gen offshwalt=.
label var offshwalt "Offshoring Potential (Blinder) from Walter"


*4-digit ISCO-code
 * Coding is based on the classification developed in Blinder, Alan. 2007. "How Many U.S. Jobs Might Be Offshorable." CEPS Working Paper No. 142.
* NOTE: all professions not listed by Blinder are coded as not offshorable (value 0)
* Unlike Goos et al, Walter considers the 4 digits. 
{
replace offshwalt=0 if iscoco<.
replace offshwalt=49 if iscoco==1142
replace offshwalt=55 if iscoco==1222
replace offshwalt=28 if iscoco==1226
replace offshwalt=55 if iscoco==1228
replace offshwalt=83 if iscoco==1231
replace offshwalt=49 if iscoco==1232
replace offshwalt=40 if iscoco==1233
replace offshwalt=53 if iscoco==1234
replace offshwalt=49 if iscoco==1235
replace offshwalt=55 if iscoco==1236
replace offshwalt=55 if iscoco==1237
replace offshwalt=55 if iscoco==1311
replace offshwalt=55 if iscoco==1312
replace offshwalt=55 if iscoco==1313
replace offshwalt=55 if iscoco==1314
replace offshwalt=55 if iscoco==1315
replace offshwalt=48 if iscoco==1316
replace offshwalt=52 if iscoco==1317
replace offshwalt=55 if iscoco==1318
replace offshwalt=55 if iscoco==1319
replace offshwalt=66 if iscoco==2111
replace offshwalt=74 if iscoco==2112
replace offshwalt=66 if iscoco==2113
replace offshwalt=66 if iscoco==2114
replace offshwalt=89 if iscoco==2121
replace offshwalt=96 if iscoco==2122
replace offshwalt=83 if iscoco==2131
replace offshwalt=90 if iscoco==2139
replace offshwalt=25 if iscoco==2141
replace offshwalt=64 if iscoco==2143
replace offshwalt=74 if iscoco==2144
replace offshwalt=72 if iscoco==2146
replace offshwalt=69 if iscoco==2147
replace offshwalt=86 if iscoco==2148
replace offshwalt=71 if iscoco==2149
replace offshwalt=81 if iscoco==2211
replace offshwalt=83 if iscoco==2212
replace offshwalt=72 if iscoco==2411
replace offshwalt=50 if iscoco==2419
replace offshwalt=74 if iscoco==2421
replace offshwalt=67 if iscoco==2444
replace offshwalt=90 if iscoco==2451
replace offshwalt=78 if iscoco==2452
replace offshwalt=25 if iscoco==2453
replace offshwalt=48 if iscoco==2455
replace offshwalt=47 if iscoco==3111
replace offshwalt=47 if iscoco==3113
replace offshwalt=47 if iscoco==3114
replace offshwalt=72 if iscoco==3115
replace offshwalt=47 if iscoco==3116
replace offshwalt=94 if iscoco==3118
replace offshwalt=54 if iscoco==3119
replace offshwalt=75 if iscoco==3121
replace offshwalt=84 if iscoco==3122
replace offshwalt=68 if iscoco==3123
replace offshwalt=36 if iscoco==3131
replace offshwalt=36 if iscoco==3132
replace offshwalt=46 if iscoco==3133
replace offshwalt=34 if iscoco==3139
replace offshwalt=52 if iscoco==3141
replace offshwalt=60 if iscoco==3152
replace offshwalt=55 if iscoco==3211
replace offshwalt=55 if iscoco==3212
replace offshwalt=44 if iscoco==3213
replace offshwalt=32 if iscoco==3228
replace offshwalt=51 if iscoco==3411
replace offshwalt=85 if iscoco==3412
replace offshwalt=50 if iscoco==3414
replace offshwalt=55 if iscoco==3416
replace offshwalt=59 if iscoco==3419
replace offshwalt=51 if iscoco==3421
replace offshwalt=48 if iscoco==3422
replace offshwalt=38 if iscoco==3431
replace offshwalt=51 if iscoco==3432
replace offshwalt=84 if iscoco==3433
replace offshwalt=84 if iscoco==3434
replace offshwalt=54 if iscoco==3439
replace offshwalt=100 if iscoco==3442
replace offshwalt=85 if iscoco==3471
replace offshwalt=30 if iscoco==3472
replace offshwalt=95 if iscoco==4111
replace offshwalt=94 if iscoco==4112
replace offshwalt=100 if iscoco==4113
replace offshwalt=71 if iscoco==4114
replace offshwalt=38 if iscoco==4115
replace offshwalt=84 if iscoco==4121
replace offshwalt=54 if iscoco==4122
replace offshwalt=31 if iscoco==4131
replace offshwalt=67 if iscoco==4132
replace offshwalt=67 if iscoco==4133
replace offshwalt=46 if iscoco==4141
replace offshwalt=26 if iscoco==4142
replace offshwalt=95 if iscoco==4143
replace offshwalt=54 if iscoco==4144
replace offshwalt=54 if iscoco==4190
replace offshwalt=94 if iscoco==4211
replace offshwalt=54 if iscoco==4214
replace offshwalt=65 if iscoco==4215
replace offshwalt=72 if iscoco==4221
replace offshwalt=54 if iscoco==4222
replace offshwalt=50 if iscoco==4223
replace offshwalt=86 if iscoco==5113
replace offshwalt=59 if iscoco==6142
replace offshwalt=36 if iscoco==7111
replace offshwalt=35 if iscoco==7112
replace offshwalt=36 if iscoco==7113
replace offshwalt=65 if iscoco==7211
replace offshwalt=69 if iscoco==7212
replace offshwalt=70 if iscoco==7213
replace offshwalt=70 if iscoco==7222
replace offshwalt=68 if iscoco==7223
replace offshwalt=68 if iscoco==7224
replace offshwalt=26 if iscoco==7311
replace offshwalt=64 if iscoco==7313
replace offshwalt=69 if iscoco==7321
replace offshwalt=69 if iscoco==7322
replace offshwalt=68 if iscoco==7323
replace offshwalt=68 if iscoco==7324
replace offshwalt=60 if iscoco==7331
replace offshwalt=75 if iscoco==7332
replace offshwalt=59 if iscoco==7341
replace offshwalt=59 if iscoco==7342
replace offshwalt=59 if iscoco==7343
replace offshwalt=34 if iscoco==7344
replace offshwalt=59 if iscoco==7345
replace offshwalt=75 if iscoco==7346
replace offshwalt=68 if iscoco==7413
replace offshwalt=27 if iscoco==7414
replace offshwalt=55 if iscoco==7415
replace offshwalt=43 if iscoco==7421
replace offshwalt=57 if iscoco==7422
replace offshwalt=57 if iscoco==7423
replace offshwalt=66 if iscoco==7424
replace offshwalt=75 if iscoco==7431
replace offshwalt=75 if iscoco==7432
replace offshwalt=75 if iscoco==7433
replace offshwalt=73 if iscoco==7434
replace offshwalt=75 if iscoco==7435
replace offshwalt=75 if iscoco==7436
replace offshwalt=57 if iscoco==7437
replace offshwalt=75 if iscoco==7441
replace offshwalt=72 if iscoco==7442
replace offshwalt=36 if iscoco==8111
replace offshwalt=36 if iscoco==8112
replace offshwalt=36 if iscoco==8113
replace offshwalt=59 if iscoco==8121
replace offshwalt=68 if iscoco==8122
replace offshwalt=70 if iscoco==8123
replace offshwalt=68 if iscoco==8124
replace offshwalt=69 if iscoco==8131
replace offshwalt=68 if iscoco==8139
replace offshwalt=57 if iscoco==8141
replace offshwalt=68 if iscoco==8142
replace offshwalt=68 if iscoco==8143
replace offshwalt=68 if iscoco==8151
replace offshwalt=70 if iscoco==8152
replace offshwalt=68 if iscoco==8153
replace offshwalt=68 if iscoco==8154
replace offshwalt=29 if iscoco==8155
replace offshwalt=68 if iscoco==8159
replace offshwalt=42 if iscoco==8161
replace offshwalt=55 if iscoco==8162
replace offshwalt=29 if iscoco==8163
replace offshwalt=64 if iscoco==8171
replace offshwalt=68 if iscoco==8172
replace offshwalt=68 if iscoco==8211
replace offshwalt=68 if iscoco==8212
replace offshwalt=68 if iscoco==8221
replace offshwalt=35 if iscoco==8222
replace offshwalt=68 if iscoco==8223
replace offshwalt=41 if iscoco==8224
replace offshwalt=29 if iscoco==8229
replace offshwalt=69 if iscoco==8231
replace offshwalt=68 if iscoco==8232
replace offshwalt=57 if iscoco==8240
replace offshwalt=58 if iscoco==8251
replace offshwalt=59 if iscoco==8252
replace offshwalt=68 if iscoco==8253
replace offshwalt=75 if iscoco==8261
replace offshwalt=75 if iscoco==8262
replace offshwalt=75 if iscoco==8263
replace offshwalt=75 if iscoco==8264
replace offshwalt=75 if iscoco==8265
replace offshwalt=75 if iscoco==8266
replace offshwalt=75 if iscoco==8269
replace offshwalt=27 if iscoco==8271
replace offshwalt=68 if iscoco==8272
replace offshwalt=68 if iscoco==8273
replace offshwalt=30 if iscoco==8274
replace offshwalt=31 if iscoco==8275
replace offshwalt=68 if iscoco==8276
replace offshwalt=27 if iscoco==8277
replace offshwalt=68 if iscoco==8278
replace offshwalt=66 if iscoco==8281
replace offshwalt=66 if iscoco==8282
replace offshwalt=66 if iscoco==8283
replace offshwalt=68 if iscoco==8284
replace offshwalt=57 if iscoco==8285
replace offshwalt=64 if iscoco==8286
replace offshwalt=68 if iscoco==8290
replace offshwalt=34 if iscoco==8340
replace offshwalt=95 if iscoco==9113
replace offshwalt=64 if iscoco==9321
replace offshwalt=29 if iscoco==9333
replace offshwalt=55 if iscoco==1227
replace offshwalt=89 if iscoco==2121
replace offshwalt=100 if iscoco==2132
replace offshwalt=70 if iscoco==2145
replace offshwalt=71 if iscoco==2149
replace offshwalt=81 if iscoco==2211
replace offshwalt=46 if iscoco==2412
replace offshwalt=50 if iscoco==2419
replace offshwalt=33 if iscoco==2432
replace offshwalt=89 if iscoco==2441
replace offshwalt=48 if iscoco==2455
replace offshwalt=72 if iscoco==3115
replace offshwalt=54 if iscoco==3119
replace offshwalt=46 if iscoco==3133
replace offshwalt=34 if iscoco==3139
replace offshwalt=34 if iscoco==3224
replace offshwalt=51 if iscoco==3411
replace offshwalt=25 if iscoco==3415
replace offshwalt=50 if iscoco==3417
replace offshwalt=59 if iscoco==3419
replace offshwalt=51 if iscoco==3432
replace offshwalt=54 if iscoco==3439
replace offshwalt=90 if iscoco==3460
replace offshwalt=54 if iscoco==4122
replace offshwalt=31 if iscoco==4131
replace offshwalt=67 if iscoco==4132
replace offshwalt=46 if iscoco==4141
replace offshwalt=26 if iscoco==4142
replace offshwalt=54 if iscoco==4222
replace offshwalt=68 if iscoco==7141
replace offshwalt=68 if iscoco==7224
replace offshwalt=65 if iscoco==7241
replace offshwalt=34 if iscoco==7344
replace offshwalt=72 if iscoco==7442
replace offshwalt=68 if iscoco==8122
replace offshwalt=68 if iscoco==8139
replace offshwalt=42 if iscoco==8161
replace offshwalt=68 if iscoco==8211
replace offshwalt=68 if iscoco==8223
replace offshwalt=41 if iscoco==8224
replace offshwalt=57 if iscoco==8240
replace offshwalt=58 if iscoco==8251
replace offshwalt=75 if iscoco==8269
replace offshwalt=66 if iscoco==8281
replace offshwalt=66 if iscoco==8283
replace offshwalt=68 if iscoco==8284
replace offshwalt=68 if iscoco==8290
replace offshwalt=70 if iscoco==9322
}

* Create Ordinal Offshwalt Variable
{
gen offshwalt4=.
replace offshwalt4=4 if offshwalt<.
replace offshwalt4=3 if offshwalt<75
replace offshwalt4=2 if offshwalt<50
replace offshwalt4=1 if offshwalt<25
label var offshwalt4 "offshwalt ordinal (4 categories)"
}

* Binary Offshwalt Variable
gen offshwalt2=offshwalt4>1
replace offshwalt2=. if offshwalt==.
label var offshwalt2 "offshwalt binary: =1 if offshwalt>=25 if offshwalt"
}
*******************************************************************************
*****4. Skill specificity provided Iversen, Cusack and Rehm (2011) ************
*******************************************************************************
{
{
*/ This code is provided by Torben Iversen; Thomas Cusack; Philipp Rehm, 2011: Risks at Work The Demand and Supply Sides of Government Redistribution
{
gen relskillspec=.	
	replace relskillspec=6.168528055	if iscoco2==11
	replace relskillspec=2.840445869	if iscoco2==12
	replace relskillspec=1.612754672	if iscoco2==13
	replace relskillspec=3.94911483		if iscoco2==21
	replace relskillspec=2.875304662	if iscoco2==22
	replace relskillspec=1.298205499	if iscoco2==23
	replace relskillspec=3.38271403		if iscoco2==24
	replace relskillspec=5.999693379	if iscoco2==31
	replace relskillspec=5.599717306	if iscoco2==32
	replace relskillspec=2.435374975	if iscoco2==33
	replace relskillspec=3.582262408	if iscoco2==34
	replace relskillspec=1.803739154	if iscoco2==41
	replace relskillspec=3.899735492	if iscoco2==42
	replace relskillspec=3.020082744	if iscoco2==51
	replace relskillspec=0.787535801	if iscoco2==52
	replace relskillspec=4.674256488	if iscoco2==61
	replace relskillspec=3.91546829		if iscoco2==71
	replace relskillspec=3.842807254	if iscoco2==72
	replace relskillspec=20.41910075	if iscoco2==73
	replace relskillspec=9.446026838	if iscoco2==74
	replace relskillspec=25.06473567	if iscoco2==81
	replace relskillspec=12.27676336	if iscoco2==82
	replace relskillspec=3.672294237	if iscoco2==83
	replace relskillspec=7.39344664		if iscoco2==91
	replace relskillspec=7.384150506	if iscoco2==92
	replace relskillspec=6.460338111	if iscoco2==93
	label var relskillspec "Relative skill specificity , Iversen"
}
	
}

}
*******************************************************************************
* 5. task categories Kurer (2020)
*******************************************************************************
{
rename iscoco iscoco_withoutchange
gen iscoco=iscoco_withoutchange
    
{
*generating 3 task categories
gen task = .
gen isco=iscoco
replace task = 1 if inlist(isco, 2411, 2431, 2441, 3411, 3471)
replace task = 1 if inrange(isco, 2100, 2213)
replace task = 1 if inrange(isco, 2443, 2444)
replace task = 1 if inrange(isco, 2446, 2452)
replace task = 1 if inrange(isco, 3100, 3212)
replace task = 1 if inrange(isco, 3433, 3440)
replace task = 1 if inrange(isco, 3442, 3444)

replace task = 2 if inlist(isco, 2442, 2445, 3226, 3432, 3441)
replace task = 2 if inrange(isco, 1000, 1319)
replace task = 2 if inrange(isco, 2220, 2410)
replace task = 2 if inrange(isco, 2412, 2430)
replace task = 2 if inrange(isco, 2432, 2440)
replace task = 2 if inrange(isco, 2453, 2470)
replace task = 2 if inrange(isco, 3213, 3220)
replace task = 2 if inrange(isco, 3222, 3224)
replace task = 2 if inrange(isco, 3229, 3410)
replace task = 2 if inrange(isco, 3412, 3429)
replace task = 2 if inrange(isco, 3449, 3470)
replace task = 2 if inrange(isco, 3472, 3480)

replace task = 3 if isco==4223
replace task = 3 if inrange(isco, 3430, 3431)
replace task = 3 if inrange(isco, 4000, 4195)
replace task = 3 if inrange(isco, 4210, 4215)

replace task = 4 if inlist(isco, 7124, 8340, 9120, 9133)
replace task = 4 if inrange(isco, 1, 110) /* departing from oesch, including 110 (armed forces). this is the actual intention of 1-100 */
replace task = 4 if inrange(isco, 6100, 7113)
replace task = 4 if inrange(isco, 7210, 8290)
replace task = 4 if inrange(isco, 9000, 9001)
replace task = 4 if inrange(isco, 9150, 9151)
replace task = 4 if inrange(isco, 9153, 9161)
replace task = 4 if inrange(isco, 9200, 9311)

replace task = 5 if inlist(isco, 5122, 5143, 9002, 9162)
replace task = 5 if inrange(isco, 7120, 7123)
replace task = 5 if inrange(isco, 7129, 7143)
replace task = 5 if inrange(isco, 8300, 8334)
replace task = 5 if inrange(isco, 9130, 9132)
replace task = 5 if inrange(isco, 9140, 9142)
replace task = 5 if inrange(isco, 9312, 9313)

replace task = 6 if inlist(isco, 3221, 3225, 4200, 9152)
replace task = 6 if inrange(isco, 3227, 3228)
replace task = 6 if inrange(isco, 4220, 4222)
replace task = 6 if inrange(isco, 5000, 5121)
replace task = 6 if inrange(isco, 5123, 5142)
replace task = 6 if inrange(isco, 5149, 5220)
replace task = 6 if inrange(isco, 9003, 9005)
replace task = 6 if inrange(isco, 9100, 9113)
replace task = 6 if inrange(isco, 9320, 9333) /* departing from oesch, including 9333 (transport labourers, animal vehicles) */

* add isco categories 2000 and 3000
* officially not defined, thus not part of oesch's categories

replace task = 1 if inlist(isco, 2000, 3000)

* task3

gen task3 = .
replace task3 = 1 if task==1 | task==2 // NRM
replace task3 = 2 if task==3 | task==4 // R
replace task3 = 3 if task==5 | task==6 // NRC
tab task3


drop isco iscoco
rename iscoco_withoutchange iscoco

}

}
}

************************************************************************
* Generate a dummy to summarize the risk of automation 
gen task3cog1=(task3==1) // dummy for non routine cognitive work - 3 task approach
replace task3cog1=. if task3==.
gen task3cog2and3=(task3==2 | task3==3) // dummy for  routine cognitive work - 3 task approach
replace task3cog2and3=. if task3==.
}

************************************************************************
********************* D. Switching *************************************
************************************************************************
{
* Here I am looking at the answer in 2014 and then comparing with the answer in 2018
// Note: 2014 and 2018 are the first years that the question about presidential election was incldued. THat is pres12 appeared for the first time in 2014 and pres16 in 2018
gen vot_12_14=pres12 if year==2014
replace vot_12_14=. if pres12~=1 & pres12~=2 & pres12~=3 
by id, sort: egen vot12_14=total(vot_12_14)


// This is my main definition of swithcing which is basically only 1 when change, but 0 in all the other cases
gen switching2 = . 
replace switching2 = 1 if vot12_14==1 & pres16==2 // Democrat to Rep
replace switching2 = 0 if vot12_14==1 & pres16==1 // Democrat to Democrat
replace switching2 = 0 if vot12_14==2 //Republicans in 2012
replace switching2 = 0 if vot12_14==1 & pres16~=2 // Democrat to anything except Rep
replace switching2 = 0 if vot12_14==3 // Other candidate

rename switching2 switching2_broad 

	
// This is a more restricted definition of swithchin
gen switching_estrict = . 
replace switching_estrict = 1 if vot12_14==1 & pres16==2 // Democrat to Rep
replace switching_estrict = 0 if vot12_14==1 & pres16==1 // Democrat to Democrat
    

}    
}
*############################################
* Saving the data
*############################################
{
* Labels
{
lab var  switching2_broad "Switching"
lab var  switching_estrict "Switching"
lab var rti "RTI"
lab var female "Female"
lab var age "Age"
lab var educ "Education"
lab var black "Black"
lab var unemployed "Unemployed"
lab var foreign "Foreign born"
lab var nonrelig "Non-Believer"
lab var offshwalt2 "Offshorability"
lab var offshwalt2 "Offshorability"
lab var relskillspec "Skill-Specificity"
lab var t2 "Task-Tech"
lab var t3 "Task-Inter"
lab var task3cog1 "Non-Routine"
lab var task3cog2and3 "Routine"


	
}	


keep id switching2_broad   rti age  female   foreign black unemployed nonrelig rincome  region  offshwalt2 relskillspec t2 t3 rincome wtssnr year educ switching_estrict task3cog2and3

save "Data\GSS.dta", replace

}
}
*##########################################
* Alternatively load prepared data
*##########################################
{
use "Data\GSS.dta", clear	
}	
*##########################################		
* Analysis		
*##########################################		
{		
{
// Figure 3: The effect of exposure to automation on vote-switching. [US Part] 		
{		
// Graph style		
grstyle clear		
set scheme s2color		
grstyle init		
grstyle set plain, nogrid		
grstyle color background white		
		
		
logit  switching_estrict  rti female age  foreign   rincome offshwalt2  relskillspec t2 t3 i.region  [pw=wtssnr] if year==2018	
		
margins, atmeans at(rti=(-1.52(0.1)2.24)) 		
     marginsplot , recast(line) recastci(rline) ci1opts(fintensity(50) lpattern(dot)) xti(Risk of automation (RTI - Index)) yti(Predicted Probability of Switching (95% CI)) ti("US") saving("Figure\US.gph", replace)		
}	
lab var  switching2_broad "Switching broad" // broad
lab var  switching_estrict "Switching strict"		// strict 			
// table A3: Switching Vote, IV - RTI, US
{
eststo clear
eststo: qui logit switching2_broad rti  female age   foreign educ i.rincome  [pw=wtssnr] if year==2018, robust				
eststo: qui logit switching2_broad rti  female age   foreign educ i.rincome offshwalt2  i.region [pw=wtssnr] if year==2018, robust	
eststo: qui logit switching2_broad rti  female age   foreign educ i.rincome offshwalt2 relskillspec  i.region [pw=wtssnr] if year==2018, robust			
eststo: qui logit switching2_broad rti  female age   foreign educ i.rincome offshwalt2 relskillspec t2 t3 unemployed black nonrelig i.region [pw=wtssnr] if year==2018, robust		

esttab , replace label se title(Switching Vote, IV - RTI, US \label {tab:rtilong})mti("+Demographic" "+Offshoring" "+Skill" "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign educ offshwalt2 relskillspec t2 t3  unemployed black nonrelig) scalars( "N Observations" "r2_p R$^2 p$" "aic AIC" ) indicate( "Income = *.rincome"  "Regional controls = *.region")

esttab using "Table\USlong_2.tex", replace label se title(Switching Vote, IV - RTI, US \label {tab:rtilong2})mti("+Demographic" "+Offshoring" "+Skill" "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign educ offshwalt2 relskillspec t2 t3  unemployed black nonrelig) scalars( "N Observations" "r2_p R$^2 p$" "aic AIC" ) indicate( "Income = *.rincome"  "Regional controls = *.region")
	
}	
// table A5: Switching Vote (alternative definition), IV - RTI		
{
eststo clear
eststo: qui logit switching_estrict rti  female age   foreign educ i.rincome  i.region [pw=wtssnr] if year==2018, robust		
eststo: qui logit switching_estrict rti  female age   foreign educ i.rincome offshwalt2  i.region [pw=wtssnr] if year==2018, robust		
eststo: qui logit switching_estrict rti  female age   foreign educ i.rincome offshwalt2 relskillspec  i.region [pw=wtssnr] if year==2018, robust		
eststo: qui logit switching_estrict rti  female age   foreign educ i.rincome offshwalt2 relskillspec t2 t3 unemployed black nonrelig i.region [pw=wtssnr] if year==2018, robust		

esttab , replace label se title(Switching Vote (alternative definition), IV - RTI, US \label {tab:rtilongstrict})mti("+Demographic" "+Offshoring" "+Skill" "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign educ offshwalt2 relskillspec t2 t3  unemployed black nonrelig) scalars( "N Observations" "r2_p R$^2 p$" "aic AIC" )  indicate( "Income = *.rincome"  "Regional controls = *.region")		

esttab using "Table\USStrict_2.tex", replace label se title(Switching Vote (alternative definition), IV - RTI, US \label {tab:rtilongstrict})mti("+Demographic" "+Offshoring" "+Skill" "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign educ offshwalt2 relskillspec t2 t3  unemployed black nonrelig) scalars( "N Observations" "r2_p R$^2 p$" "aic AIC" )  indicate( "Income = *.rincome"  "Regional controls = *.region")		
	
}		
// All table summarized. 
{
// table A6: Switching Vote, IV - Routine (dummy), US		
eststo clear
// Routine Dummy
eststo: qui logit switching2_broad task3cog2and3  female age   foreign educ i.rincome  i.region [pw=wtssnr] if year==2018, robust		
eststo: qui logit switching2_broad task3cog2and3  female age   foreign educ i.rincome offshwalt2  i.region [pw=wtssnr] if year==2018, robust
eststo: qui logit switching2_broad task3cog2and3  female age   foreign educ i.rincome offshwalt2 relskillspec i.region [pw=wtssnr] if year==2018, robust		
		
eststo: qui logit switching2_broad task3cog2and3  female age   foreign educ i.rincome offshwalt2 relskillspec t2 t3 unemployed black nonrelig i.region [pw=wtssnr] if year==2018, robust		

esttab , replace label se title(Switching Vote, IV - Routine (dummy), US \label {tab:task3cog2and3})mti("+Demographic" "+Offshoring" "+Skill" "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(task3cog2and3  female age   foreign educ offshwalt2 relskillspec t2 t3  unemployed black nonrelig) scalars( "N Observations" "r2_p R$^2 p$" "aic AIC" ) indicate( "Income = *.rincome"  "Regional controls = *.region")		

esttab using "Table\USdummy_2.tex", replace label se title(Switching Vote, IV - Routine (dummy), US \label {tab:task3cog2and3})mti("+Demographic" "+Offshoring" "+Skill" "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(task3cog2and3  female age   foreign educ offshwalt2 relskillspec t2 t3  unemployed black nonrelig) scalars( "N Observations" "r2_p R$^2 p$" "aic AIC" ) indicate( "Income = *.rincome"  "Regional controls = *.region")		




	
}
	
}
}		
*##########################################		
* Descriptive 		
*##########################################		
{		
		
// table A1: Descriptive statistic: USA GSS 2016 vs 2012		
{		
	eststo clear

qui estpost sum switching2_broad   rti age  female   foreign black unemployed nonrelig rincome  region  offshwalt2 relskillspec t2 t3  [w=wtssnr] if year==2018, d		

 	
esttab ,  /// ,  ,		
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(0)) max(label(Max) fmt(0)) count(label(Obs.) fmt(0))") ///	
	nonumber label replace noobs varlabels(switching2_broad "Vote Switching" rti "RTI" age "Age" female "Female" foreign "Foreign born" black "Black" unemployed "Unemployed" nonrelig "Non-Believer" rincome "Income Level" offshwalt2 "Offshorability" relskillspec "Skill-Specificity" t2 "Task-Tech" t3 "Task-Inte") nomtitle 	
		
esttab using  "Table\summarystats_US.tex",  /// ,  ,		
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(0)) max(label(Max) fmt(0)) count(label(Obs.) fmt(0))") ///	
	nonumber label replace noobs varlabels(switching2_broad "Vote Switching" rti "RTI" age "Age" female "Female" foreign "Foreign born" black "Black" unemployed "Unemployed" nonrelig "Non-Believer" rincome "Income Level" offshwalt2 "Offshorability" relskillspec "Skill-Specificity" t2 "Task-Tech" t3 "Task-Inte") nomtitle 	
}		
}		
